package com.xmb.wcapi.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmb.wcapi.entity.SysDept;
import com.xmb.wcapi.entity.SysRole;
import com.xmb.wcapi.entity.SysUser;
import com.xmb.wcapi.mapper.SysUserMapper;
import com.xmb.wcapi.service.ISysDeptService;
import com.xmb.wcapi.service.ISysRoleService;
import com.xmb.wcapi.service.ISysUserService;
import com.xmb.wcapi.util.ConditionUtil;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangping
 * @since 2020-03-11
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

	@Autowired
	private ISysDeptService iSysDeptService;
	
	@Autowired
	private ISysRoleService iSysRoleService;
	
	@Override
	public List<SysUser> selectInfoList(Wrapper<SysUser> ew) {
		return list(ew);
	}

	@Override
	public SysUser selectInfoOne(Wrapper<SysUser> ew) {
		return getOne(ew);
	}

	@Override
	public IPage<SysUser> selectInfoPage(Page<SysUser> page, QueryWrapper<SysUser> ew, String dept, String role) throws Exception{
		if(StringUtils.isNotEmpty(dept)) {
			List<SysDept> deptList = iSysDeptService.list(ConditionUtil.convert(dept, new QueryWrapper<SysDept>()));
			Set<String> deptIds = new HashSet<String>();
			deptList.stream().forEach(e -> deptIds.add(e.getId()));
			ew.in("department_id", deptIds);
		}
		if(StringUtils.isNotEmpty(role)) {
			List<SysRole> roleList = iSysRoleService.list(ConditionUtil.convert(dept, new QueryWrapper<SysRole>()));
			Set<String> roleIds = new HashSet<String>();
			roleList.stream().forEach(e -> {
				roleIds.add(e.getId());
			});
			ew.in("role_id", roleIds);
		}
		return page(page,ew.orderByAsc("id"));
	}

	@Override
	public void add(SysUser user) throws Exception {
		save(user);
	}

	@Override
	public void edit(SysUser user) throws Exception {
		updateById(user);
	}

	@Override
	public void deleteInfo(List<String> ids) throws Exception {
		removeByIds(ids);
	}

}
